package com.theme.passport.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.annotation.Login;
import com.theme.passport.controller.app.utils.AppContextUtil;
import com.theme.passport.entity.UserEntity;
import com.theme.passport.entity.constant.UserWarpVo;
import com.theme.passport.entity.theme.UserFollowEntity;
import com.theme.passport.service.UserService;
import com.theme.passport.service.theme.UserFollowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@RequestMapping("app/user-follow")
@Api("用户的关注和粉丝")
public class UserFollowController {

    @Autowired
    private UserFollowService userFollowService;

    @Autowired
    private UserService userService;

    @Login
    @GetMapping("/fansList")
    @ApiOperation("查询用户的粉丝接口")
    public R fansList(@RequestParam Map<String, Object> params){
        UserEntity currentUser = AppContextUtil.getCurrentUser();
        params.put("userId",currentUser.getId() +"");
        PageUtils page = userFollowService.queryPage(params);
        List<UserFollowEntity> list = (List<UserFollowEntity>) page.getList();
        list.forEach(userFollowEntity -> {
            UserEntity user = userService.getById(userFollowEntity.getFansId());
            UserWarpVo userWarpVo = new UserWarpVo();
            BeanUtils.copyProperties(user,userWarpVo);
            userFollowEntity.setUserWarpVo(userWarpVo);
        });
        page.setList(list);
        return R.ok().put("page", page);
    }

    @Login
    @GetMapping("/focusonList")
    @ApiOperation("查询查询用户的关注接口")
    public R focusonList(@RequestParam Map<String, Object> params){
        UserEntity currentUser = AppContextUtil.getCurrentUser();
        params.put("fansId",currentUser.getId() +"");
        PageUtils page = userFollowService.queryPage(params);
        List<UserFollowEntity> list = (List<UserFollowEntity>) page.getList();
        list.forEach(userFollowEntity -> {
            UserEntity user = userService.getById(userFollowEntity.getUserId());
            UserWarpVo userWarpVo = new UserWarpVo();
            BeanUtils.copyProperties(user,userWarpVo);
            userFollowEntity.setUserWarpVo(userWarpVo);
        });
        page.setList(list);
        return R.ok().put("page", page);
    }

    @Login
    @PostMapping("/focuson")
    @ApiOperation("用户点击关注接口")
    public R focuson(@RequestBody UserFollowEntity userFollow){
        UserEntity currentUser = AppContextUtil.getCurrentUser();
        QueryWrapper<UserFollowEntity> orderWrapper = new QueryWrapper<>();
        orderWrapper.and(wrapper -> wrapper.eq("fans_id", currentUser.getId()).eq("user_id", userFollow.getUserId()));
        List<UserFollowEntity> list = userFollowService.list(orderWrapper);
        //不存在，新增用户关注
        if (list != null && list.size()>0){
            //已存在，则取消关注
            UserFollowEntity userFollowEntity = list.get(0);
            List<Long> ids = new ArrayList<>();
            ids.add(userFollowEntity.getId());
            userFollowService.removeByIds(ids);
            return R.ok();
        }
        userFollow.setCreated(new Date());
        userFollow.setUserId(currentUser.getId());
		userFollowService.save(userFollow);
        return R.ok();
    }


    @Login
    @PostMapping("/unFocuson")
    @ApiOperation("用户取消关注接口")
    public R unFocuson(@RequestBody UserFollowEntity userFollow){
        UserEntity currentUser = AppContextUtil.getCurrentUser();
        QueryWrapper<UserFollowEntity> orderWrapper = new QueryWrapper<>();
        orderWrapper.and(wrapper -> wrapper.eq("fans_id", currentUser.getId()).eq("user_id", userFollow.getUserId()));
        List<UserFollowEntity> list = userFollowService.list(orderWrapper);
        if (list != null && list.size()>0){
            //已存在，则取消关注
            UserFollowEntity userLikeEntity = list.get(0);
            List<Long> ids = new ArrayList<>();
            ids.add(userLikeEntity.getId());
            userFollowService.removeByIds(ids);
            return R.ok();
        }
        return R.ok();
    }

    @Login
    @PostMapping("/delete")
    @ApiOperation("删除记录接口 id数组")
    public R delete(@RequestBody Long[] ids){
        userFollowService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
